using System;
using System.Collections.Generic;
using System.Text;

namespace GetTogether.Studio
{
    public class ProjectHistory
    {
        private DateTime _RecentUsed;

        public DateTime RecentUsed
        {
            get { return _RecentUsed; }
            set { _RecentUsed = value; }
        }

        private DateTime _RecentUsedAction;

        public DateTime RecentUsedAction
        {
            get { return _RecentUsedAction; }
            set { _RecentUsedAction = value; }
        }

        public static void SaveHistory(ProjectParameter.ProjectCategory prjCategory, string prjName, string username)
        {
            SaveHistory(prjCategory, prjName, username, DateTime.Now, DateTime.MinValue);
        }

        public static void SaveHistory(ProjectParameter.ProjectCategory prjCategory, string prjName, string username, DateTime recentUsed, DateTime recentUsedAction)
        {
            string path = GetTogether.Studio.ProjectParameter.GetSettingsPath(prjCategory, username);
            GetTogether.Studio.ProjectHistory ph = GetProjectHistory(path, prjName);
            if (ph == null)
            {
                ph = new GetTogether.Studio.ProjectHistory();
            }
            if (recentUsed != DateTime.MinValue) ph.RecentUsed = recentUsed;
            if (recentUsedAction != DateTime.MinValue) ph.RecentUsedAction = recentUsedAction;
            path = GetProjectHistoryFile(path, prjName, true);
            GetTogether.Utility.FileHelper.SerializeToFile(ph, path);
        }

        public static string GetProjectHistoryFile(string path, string projectName, bool isCreateWhenNotExists)
        {
            string dir = string.Concat(System.IO.Path.Combine(path, projectName), "(History)");
            if (isCreateWhenNotExists && !System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
            return string.Concat(dir, "/", projectName, ".History");
        }

        public static ProjectHistory GetProjectHistory(string path, string projectName)
        {
            try
            {
                string historyFile = GetProjectHistoryFile(path, projectName, false);
                GetTogether.Studio.ProjectHistory ph = null;
                if (System.IO.File.Exists(historyFile))
                {
                    ph = GetTogether.Utility.SerializationHelper.FromXml<ProjectHistory>(System.IO.File.ReadAllText(historyFile));
                }
                return ph;
            }
            catch (Exception ex)
            {
                GetTogether.Utility.LogHelper.Write(GetTogether.Utility.LogHelper.LogTypes.Error, ex.ToString());
                return null;
            }
        }
    }
}
